package team.seekers.hula.question.point.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.conditions.query.LambdaQueryChainWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import team.seekers.hula.common.entity.VisibilityUpdateDTO;
import team.seekers.hula.common.entity.enums.StatusEnum;
import team.seekers.hula.record.mind.service.MindService;
import team.seekers.hula.question.answer.service.AnswerManageService;
import team.seekers.hula.question.point.entity.dao.PointDO;
import team.seekers.hula.question.point.entity.dto.PointQueryDTO;
import team.seekers.hula.question.point.entity.dto.PointUpdateManageDTO;
import team.seekers.hula.question.point.entity.vo.PointManageListVO;
import team.seekers.hula.question.point.mapper.PointMapper;
import team.seekers.hula.question.question.service.QuestionManageService;
import team.seekers.hula.question.question.service.QuestionService;
import team.seekers.hula.user.user.entity.vo.UserBasicInfoVO;
import team.seekers.hula.user.user.service.UserService;

import java.util.*;

/**
 * 关于问题的一个观点 后台管理
 * @author hbc
 * @since 2021-02-15
 */
@Service
public class PointManageService extends ServiceImpl<PointMapper, PointDO> {

    @Autowired
    private UserService userService;

    @Autowired
    private MindService mindService;


    @Autowired
    private QuestionManageService questionManageService;

    @Autowired
    private QuestionService questionService;


    @Autowired
    private AnswerManageService answerManageService;



    // 管理式删除，直接删除所有
    public void delete(String pointId) {
        //删除所有回答
        answerManageService.deleteByPointId(pointId);

        // 维护mind表
        mindService.deleteMindByLinkId(pointId);

        // 删除观点
        removeById(pointId);
    }

    // 管理式删除，直接删除所有
    public void deleteByQuestionId(String questionId) {
        LambdaQueryChainWrapper<PointDO> wrapper = lambdaQuery().eq(PointDO::getQuestionId, questionId);

        List<PointDO> pointDOList = wrapper.select(PointDO::getId).list();
        ArrayList<String> pointIds = new ArrayList<>();
        for (PointDO pointDO : pointDOList) {
            pointIds.add(pointDO.getId());
        }

        //删除所有回答
        answerManageService.deleteByPointIds(pointIds);

        // 维护mind表
        mindService.deleteMindByLinkIds(pointIds);

        // 删除观点
        remove(wrapper);
    }


    @Transactional(rollbackFor = Exception.class)
    public void update(PointUpdateManageDTO updateVO) {
        PointDO pointDO = getById(updateVO.getId());
        BeanUtils.copyProperties(updateVO, pointDO);
        updateById(pointDO);
    }

    @Transactional(rollbackFor = Exception.class)
    public void updateVisibility(VisibilityUpdateDTO updateDTO) {
        lambdaUpdate().set(PointDO::getVisibility,updateDTO.getVisibility())
                .eq(PointDO::getId,updateDTO.getId())
                .update();

        mindService.updateVisibilityByLinkId(updateDTO.getId(),updateDTO.getVisibility());
    }



    public List<PointManageListVO> list(PointQueryDTO pointQueryDTO){
        QueryWrapper<PointDO> pointDOQueryWrapper = new QueryWrapper<>();

        if(StringUtils.isNotBlank(pointQueryDTO.getNickname())){
            //如果用户名不为空，需要查询符合条件的userid
            List<String> userIds = userService.listIdsLikeNickname(pointQueryDTO.getNickname());
            if(userIds.size() > 0){
                pointDOQueryWrapper.in("user_id",userIds);
            }else{
                return new ArrayList<>();
            }
        }

        if (StringUtils.isNotBlank(pointQueryDTO.getKeyword())){
            pointDOQueryWrapper.like("keyword",pointQueryDTO.getKeyword());
        }

        if (pointQueryDTO.getStatus()!=null){
            pointDOQueryWrapper.eq("status",pointQueryDTO.getStatus());
        }

        if(StringUtils.isNotBlank(pointQueryDTO.getId())){
            pointDOQueryWrapper.eq("id",pointQueryDTO.getId());
        }
        if(StringUtils.isNotBlank(pointQueryDTO.getQuestionId())){
            pointDOQueryWrapper.eq("question_id",pointQueryDTO.getQuestionId());
        }
        List<PointDO> list = list(pointDOQueryWrapper);

        ArrayList<PointManageListVO> pointManageListVOS = new ArrayList<>();
        for (PointDO record : list) {
            PointManageListVO pointManageListVO = new PointManageListVO();
            BeanUtils.copyProperties(record,pointManageListVO);

            UserBasicInfoVO userBasicInfoVO = userService.getBasicInfoVO(record.getUserId());
            pointManageListVO.setNickname(userBasicInfoVO.getNickname());
            pointManageListVO.setAvatar(userBasicInfoVO.getAvatar());

            pointManageListVO.setStatus(record.getStatus().getName());
            pointManageListVOS.add(pointManageListVO);
        }
        return pointManageListVOS;
    }

    public void changeApproval(String id,Integer status){
        PointDO pointDO = getById(id);
        if(pointDO==null){
            return ;
        }
        //判断重复审批
        if(pointDO.getStatus().getType()==1||pointDO.getStatus().getType()==2){
            return ;
        }
        pointDO.setStatus(StatusEnum.getByType(status));
        updateById(pointDO);
    }

}
